#!/usr/bin/perl -w
use strict;
use English;
use Getopt::Long;
use File::stat;
use POSIX qw(ceil floor);

my $DISK_SN_SAVE_FILE = "/var/OPM/scripts/DiskSN.txt";
my ($dir, $usage, $base, $checksum);

my (%SNINFO,%SNPATHS,%SNSHORTPATHS,%SNSLICES,%SNSLICEUSAGE,%PATHSSN,%SNVERITAS);

#GetOptions("dir=s"=>\$dir, "sum" => \$checksum)||die "$usage";
#$usage = "$PROGRAM_NAME ";
#unless( defined($dir)) {die "$usage";}

my $hostname = `/bin/uname -n`; chomp $hostname;
my $hostid = `/usr/bin/hostid`; chomp $hostid;
my $dt = `/bin/date +"%Y-%m-%d %H:%M:%S"`; chomp $dt;

my $outfile = "/var/OPM/data/${hostname}_disks.out";
open (OUT, ">$outfile");
print OUT '<uname -n>',"\n",$hostname,"\n",'</uname -n>', "\n";
print OUT '<hostid>',"\n",$hostid,"\n",'</hostid>', "\n";
print OUT '<date>',"\n",$dt,"\n",'</date>', "\n";

GetDiskInf();
PrintFSInfo();
GetVxDg();
close OUT;

sub PrintFSInfo{
	print OUT "<fs>\n";
	{
	my @TMP = `/bin/df -kP |/bin/egrep -v "^fd |^/proc |^mnttab |^tmpfs|Filesystem|^Filesystem \*kbytes"`;
	foreach my $tmp (@TMP){
		chomp $tmp;
		$tmp =~ s/^\s+//; $tmp =~ s/\s+$//; $tmp =~ s/\s+/\t/g;
		my($fs,$kb,$used,$free,$cap,$mp) = split /\t/,$tmp;
		print OUT "$fs\t$kb\t$used\t$free\t$cap\t$mp\n";
	}
	}
	print OUT "</fs>\n";
}

sub GetVxDg{
        print OUT "<dg>\n";
               my $dgfree;
               my $dgstate;
               my $dgid;
					my $s_DGLIST = `/usr/sbin/vgdisplay -c`;
					$s_DGLIST =~ s/\s+//;
					my @a_DGLIST = split /\n/, $s_DGLIST;
               foreach my $dgname (@a_DGLIST){
                        chomp $dgname; $dgname =~ s/\s+//g;
								my $dgstate = "online";
								my @a_vg = split /:/, $dgname;
								my $s_dg_name = $a_vg[0];
								my $s_dg_uuid = $a_vg[16];
								my $s_dg_free = $a_vg[15]; $s_dg_free = $s_dg_free * $a_vg[12] / 1024 / 1024;
								my $s_dg_size = $a_vg[11]; $s_dg_size = $s_dg_size / 1024 / 1024;
                        #open(DATA, "/usr/sbin/lsvg $dgname|") || die "erreur";
                        print OUT "$s_dg_name\t$dgstate\t$s_dg_uuid\t$s_dg_free\n";
                }
        print OUT "</dg>\n";
}

sub GetDiskInf {
	print OUT "<disk>\n";
	my @TMP = `/sbin/sfdisk -lnq|grep Disk`; # Disk /dev/sda: 17833 cylinders, 255 heads, 63 sectors/track
	my @NEW = @TMP;
       
	my $disksize = 0;
	my $path;
	my $pv;
	foreach my $tmp (@TMP){
        	if($tmp =~ /^Disk (.*): (.*)/){
			$path		= $1;
			$disksize	= `/sbin/sfdisk -snq $1`;chomp($disksize);
			$disksize	= ($disksize * 1024);
			$pv = $path; if ( $pv =~ /\/dev\/(.*)/ ){ $pv = $1; }
         	}
		my $sn		= "$hostid$path";$sn =~ s/\//-/g;
		my $dgname = "-";
  		my $disktype = "-";
  		my $diskusage = "$disksize";
  		my $status 	= "online";
		if ($disksize > 0 ) {
			print OUT "$pv\t$sn\t$dgname\t$disktype\t$status\t$path\t$disksize\n";
		}
	}
        print OUT "</disk>\n";
}
`chown saunix $outfile`;
